package com.jintoufs.web.action.support;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jintoufs.service.baseinfo.CurrencyExchangeService;
import org.apache.commons.lang.StringUtils;

import com.jintoufs.domain.user.User;

public class SupportAction {
	@Resource
	private CurrencyExchangeService currencyExchangeService;
	/**
	 * 获得String类型请求参数
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	public String getString(HttpServletRequest request, String name) {
		if (StringUtils.isNotEmpty(name)) {
			return request.getParameter(name);
		}
		return null;
	}

	/**
	 * 获得Integer类型请求参数
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	public int getInteger(HttpServletRequest request, String name) {
		String value = getString(request, name);
		if (StringUtils.isNotEmpty(value)) {
			try {
				return Integer.parseInt(value);
			} catch (Exception e) {
				return 0;
			}

		}
		return 0;
	}

	/**
	 * 获得Double类型请求参数
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	public double getDouble(HttpServletRequest request, String name) {
		String value = getString(request, name);
		if (StringUtils.isNotEmpty(value)) {
			try {
				return Double.parseDouble(value);
			} catch (Exception e) {
				e.printStackTrace();
				return 0;
			}

		}
		return 0;
	}

	/**
	 * 获得Boolean类型请求参数
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	public boolean getBoolean(HttpServletRequest request, String name) {
		String value = getString(request, name);
		if (StringUtils.isNotEmpty(value)) {
			try {
				return Boolean.getBoolean(value);
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}

		}
		return false;
	}

	/**
	 * 向客户端写数据
	 * 
	 * @param response
	 * @param info
	 */
	public void writeInfo(HttpServletResponse response, String info) {
		try {
			response.setContentType("text/html;charset=UTF-8");
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			response.getWriter().write(info);
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected void showBox(HttpServletResponse response, String context) {
		try {
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			out.print("<script>alert('" + context + "');history.go(-1);</script>");
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static User getCurrentUserInSession(HttpSession session) {
		return (User) session.getAttribute("user");
	}

	public static void putCurrentUserToSession(HttpSession session, User user) {
		session.setAttribute("user", user);
	}

	public String formatDate(Date date) {
		String dateStr = null;
		SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateStr = sdFormat.format(date);
		return dateStr;
	}

	public Date parseString(String time) throws ParseException {
		SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
		return sdFormat.parse(time);
	}

	public double getPrice(String uniteCode, List<Map<String, Object>> coins) {
		double price = 0;
		//不是虚拟比计价的，转换成cny
		if (!"BTC".equalsIgnoreCase(uniteCode) && !"ETH".equalsIgnoreCase(uniteCode)&&!"BNB".equalsIgnoreCase(uniteCode)) {
			if (!"CNY".equalsIgnoreCase(uniteCode)) {
				double rate = currencyExchangeService.queryRate(uniteCode, "CNY");
				price = rate;
			} else {
				price = 1;
			}
		} else {
			//是虚拟币计价的，找到当前网站的计价单位币种的法币价格，然后再转换成cny
			if (!coins.isEmpty()) {
				Map<String, Object> unitCoin = null;
				for (Map<String, Object> tmp : coins) {
					if (tmp.get("unit_code").toString().equalsIgnoreCase("BNB")||tmp.get("unit_code").toString().equalsIgnoreCase("ETH") || tmp.get("unit_code").toString().equalsIgnoreCase("BTC")) {

					} else {
						if (uniteCode.equalsIgnoreCase(tmp.get("coin_rename").toString())) {
							unitCoin = tmp;
							break;
						}
					}
				}
				if (unitCoin != null) {
					double unitePrice = Double.parseDouble(unitCoin.get("last").toString());
					String uniteCodeTmp = unitCoin.get("unit_code").toString();
					if (!"CNY".equalsIgnoreCase(uniteCodeTmp)) {
						double rate = currencyExchangeService.queryRate(uniteCodeTmp, "CNY");
						price = unitePrice * rate;
					} else {
						price = unitePrice;
					}
				}
			}
		}
		return price;

	}
}
